define(['utils', 'template', 'nicescroll', 'dialogTools', 'countDown'], function (utils, temp, ns) {
    var global = window.$CONFIG || {};
    var WallId = window.$CONFIG['wallid'];

    var waitPeopleMap = {};

    var waitPeopleNum = 0;
    var waitPeopleFlag = false;
    var waitPeopleIndex = -1;

    var maxWaitPeopleNum = 28;

    // var BonusConfig = {
    //     ajaxUrl: $CONFIG.REMOTE.WallURL+'/ajax',
    //     testUrl: ''
    // };

    var bigScreenBonus = {

        init: function (opt) {
            var that = this;

            bigScreenBonus.bonusGaming.bonusInitListData();
            that.bounsInit();
            that.bindEvent();
            that.bonusSlideInit();

        },
        bindEvent: function () {
            var _this = this;

            $('#bonusDetail').on('click', '.item-dropdown > a', function (e) {
                $(this).parent().find('.menu-dropdown').slideDown(200);
                e.stopPropagation();
            });
            $('#bonusDetail').on('click', function (e) {
                $('#bonusDetail').find('.item-dropdown .menu-dropdown').slideUp(100);
            });

            $("#bonusDetail").on("click", '.item-dropdown .menu-dropdown li', function () {
                var $this = $(this);
                
                var luckyTitle = $this.data("luckytitle"),
                    luckyid = $this.data("luckyid"),
                    shakestatus = $this.data("shakestatus"),
                    luckyIndex = $this.index();

                $("#bonusSel .sel-txt").html(luckyTitle);
                
                bigScreenBonus.bonusGaming.$el.luckyindex = luckyIndex;
                bigScreenBonus.bonusGaming.$el.luckyid = luckyid;
                bigScreenBonus.bonusGaming.$el.shakestatus = shakestatus;

                if (luckyIndex != waitPeopleIndex) {
                    waitPeopleIndex = luckyIndex;
                    waitPeopleFlag = true;
                }
                
                bigScreenBonus.bonusWait.bonusWaitGetStatus();
                
            });

            $('.content-bonus').on('click', '.bonus-startBtn', function () {
                bigScreenBonus.bonusGaming.startbtn = true;
                bigScreenBonus.bonusWait.bonusWaitGetStatus();
                
            });
            $('.content-bonus').on('click', '.bonus-startBtn2', function () {
                utils.ajax({
                    url: hb_chongzhi,
                    data: {
                        luckyId: 1,
                        topn: 50
                    },
                    success: function (data) {
                        if (data.success == 1) {
                        location.reload()
                        } else {
                            alert(idata.msg);
                        }
                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    },
                });

            });
            
            $('.content-bonus').on('click', '.bonusClose', function () {
                $('#bonus-prise-layout').hide();
                $('#bonusDetail').html('');
                $("#bonusDetail").removeClass('prizeshow');
                bigScreenBonus.bounsInit();
                bigScreenBonus.bonusWait.bonusCutStartBtn(2);
                bigScreenBonus.bonusGaming.luckyinitindex = 1;
                $('.bonus-startBtn2').show();
                // bigScreenBonus.bonusSlideInit();
                
            });
        },
        bonusSlideInit: function () {

            utils.ajax({
                url: Get_RedPackLuckies,
                data: {
                    wallId: 0
                },
                success: function (data) {
                    // console.log(data);
                    //暂时注释
                    // if (data.successful2) {
                    if (data.successful2) {
                        if(data.gameover==1){
                            //游戏结束
                            bigScreenBonus.bonusGaming.bonusGetPrize();
                            bigScreenBonus.bonusGaming.btnstatus = true;
                            bigScreenBonus.bonusWait.bonusCutStartBtn(2);
                        }

                    }
                },
                error: function () {
                    $.dialog.tips({
                        status: "warning",
                        content: '糟糕，早知道不这样干了！！'
                    });
                }
            });

        },
        bonusWait:{
            bonusWaitGetStatus: function () {

                var thisLuckyId = bigScreenBonus.bonusGaming.$el.luckyid;
                var luckyinitIndex = bigScreenBonus.bonusGaming.luckyinitindex;
                var thisLuckyId = 123;
                if (!thisLuckyId) {
                    $.dialog.tips({
                        status: "warning",
                        content: '未配置红包雨或未支付！'
                    });
                } else {
                        utils.ajax({
                        url: Get_RedPackLucky,
                        data: {
                            luckyId: luckyinitIndex
                        },
                        loadingObj: {
                            el: $(".content-bonus"),
                            type: "belt",
                            msg:"正在加载中"
                        },
                        success: function (data) {
                            // console.log(data);
                            var drawtime = data;
                            bigScreenBonus.bonusGaming.$el.drawtime = drawtime.DrawTime;
                            bigScreenBonus.bonusGaming.$el.shakestatus = drawtime.ShakeStatus;

                            if (bigScreenBonus.bonusGaming.$el.shakestatus == 0) {
                                bigScreenBonus.bonusGaming.btnstatus = false;
                                bigScreenBonus.bonusWait.bonusCutStartBtn('');
                                if (!bigScreenBonus.bonusGaming.startbtn)
                                    bigScreenBonus.bonusWait.bonusWaitRushStatus();

                            } else if (bigScreenBonus.bonusGaming.$el.shakestatus == 1) {
                                bigScreenBonus.bonusGaming.btnstatus = false;
                                bigScreenBonus.bonusWait.bonusCutStartBtn('');
                                if (!bigScreenBonus.bonusGaming.startbtn){
                                    // console.log(1);
                                    bigScreenBonus.bonusWait.bonusHoldArea();
                                } else {
                                    // console.log(2);
                                    bigScreenBonus.bonusGaming.startbtn = false;
                                    bigScreenBonus.bonusGaming.$flag = true;
                                    bigScreenBonus.bonusGaming.bonusStartEvent();
                                }
                            } else if (bigScreenBonus.bonusGaming.$el.shakestatus == 2) {
                                var date = new Date();
                                bigScreenBonus.bonusGaming.$flag = true;
                                bigScreenBonus.bonusGaming.startbtn = false;
                                // var startime = drawtime.StarTime.replace(/[^0-9]/g, '');
                                var endtime = parseInt(date.getTime());
                                bigScreenBonus.bonusGaming.$el.startime =  drawtime.StarTime;
                                bigScreenBonus.bonusGaming.$el.endtime = endtime;

                                var difftime = (bigScreenBonus.bonusGaming.$el.endtime
                                    - bigScreenBonus.bonusGaming.$el.startime*1000) / 1000;
                                console.log(difftime);
                                var remaintime = bigScreenBonus.bonusGaming.$el.drawtime - difftime;

                                if (difftime < bigScreenBonus.bonusGaming.$el.drawtime) {
                                    //切入游戏中断-进行
                                    bigScreenBonus.bonusGaming.breakflag = true;
                                    bigScreenBonus.bonusGaming.temptime = remaintime;
                                    bigScreenBonus.bonusGaming.bonusStartEvent();
                                } else {
                                    bigScreenBonus.bonusGaming.rainstatus = false;
                                    bigScreenBonus.bonusGaming.bonusEndStatus();
                                }

                            } else if (bigScreenBonus.bonusGaming.$el.shakestatus == 3) {
                                bigScreenBonus.bonusGaming.bonusGetPrize();
                                bigScreenBonus.bonusGaming.btnstatus = true;
                                bigScreenBonus.bonusWait.bonusCutStartBtn(2);

                            }
                        },
                        error: function () {
                            $.dialog.tips({
                                status: "warning",
                                content: '网络异常，获取信息失败!请重试！'
                            });
                        }
                    });
                }
               
            },
            bonusWaitRushStatus:function(){
                utils.ajax({
                    url: BonusConfig.ajaxUrl + '/SetRedPackReady',
                    data: {
                        wallId:WallId,
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid
                    },
                    success: function (data) {
                        if (data.successful) {
                            if (data.code == 200) {
                                bigScreenBonus.bonusGaming.$el.shakestatus = 1;
                                bigScreenBonus.bonusWait.bonusHoldArea();
                            }
                        }
                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    }
                });
            },
            bonusWaitRush:function(){
                waitPeopleNum = 0;
                waitPeopleMap = {};
                $('.bonusHoldArea ul').html('');
            },
            bonusCutStartBtn:function(opts){
                $('.bonus-startBtn').css({
                    "background": "url(../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-startBtn"+opts+".png) no-repeat center",
                    "background-size": '100%'
                });
            },
            fullPath: function (src) {
                if (!src || src == '') {
                    return CONFIG.URL.StaticAll + "/img/wall/wechat_userico.png";
                } else {
                    if (src.indexOf("http://") >= 0 || src.indexOf("https://") >= 0)
                        return src;
                    else {
                        return CONFIG.URL.file + src;
                    }
                }
            },
            bonusHoldArea: function () {
                var that = this;
                var bonusArea = temp.render('area-bonus-tpl', {
                });

                if ($('.bonusHoldArea').html() == undefined) {
                    $('.bonus-stage').html(bonusArea);
                }
                // console.log(3);
                // console.log(waitPeopleFlag);
                if (waitPeopleFlag) {
                    setTimeout(function () {
                        waitPeopleFlag = false;
                        that.bonusWaitRush();
                        bigScreenBonus.bonusWait.bonusWaitInfo('.bonusHoldArea ul');
                    },1000);
                }
                
            },
            bonusWaitInfo: function (elem) {
                utils.ajax({
                    url: BonusConfig.ajaxUrl + '/gettopn',
                    data: {
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid,
                        topn: 50
                    },
                    success: function (data) {
                        if (data.successful) {
                            var json = JSON.parse(data.data);
                            var list = json.users;
                            var imgStr = [];
                            
                            if (list.length <= 0) {
                                //$(elem).html('');
                                return;
                            }
                            
                            if (json.data == 200) {

                                if ($('#bonusWaitNum').html() !== undefined)
                                    $('#bonusWaitNum').html(json.total);

                                
                                for (var i = 0; i < list.length; i++) {
                                    if (i >= 28) break;
                                    var user = list[i];

                                    //var str = '';
                                    //str += user.FacePath;

                                    if (!waitPeopleMap[user.UserId]) {
                                        var photo = bigScreenBonus.bonusWait.fullPath(user.FacePath);
                                        imgStr.push('<li><img src="' + photo + '" /></li>');
                                        waitPeopleNum++;
                                        waitPeopleMap[user.UserId] = true;
                                    }

                                }

                                if (waitPeopleFlag) {
                                    bigScreenBonus.bonusWait.bonusWaitRush();
                                    return;
                                }

                                $(elem).prepend(imgStr.join(''));

                                var duration = waitPeopleNum - maxWaitPeopleNum;

                                if (duration > 0) {
                                    for (var i = 0; i < duration; i++) {
                                        $(elem).find('li:last-child').remove();
                                        waitPeopleNum = maxWaitPeopleNum;
                                    }
                                }
                            }
                            
                        }
                        
                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                        if (!waitPeopleFlag) {
                            setTimeout(function () {
                                bigScreenBonus.bonusWait.bonusWaitInfo(elem);

                            }, 1000);
                        }
                    },
                    complete: function () {
                        if (!waitPeopleFlag) {
                            setTimeout(function () {
                                bigScreenBonus.bonusWait.bonusWaitInfo(elem);

                            }, 1000);
                        }
                    }
                });
            }
        },
        bounsInit:function(){
            
            var bonusInit = temp.render("init-bonus-tpl", {
                joinPeople: '已加入',
                joinUnit: '人'
            });
            $('#bonusDetail').append(bonusInit);
            if (bigScreenBonus.bonusGaming.btnstatus)
                bigScreenBonus.bonusWait.bonusCutStartBtn(2);
            $('#bonusDetail').css({
                "background": "url(../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-bg.png) no-repeat center",
                "backgroundSize": "100%"
            });
        },
        bonusGaming:{
            $el: {},
            $flag: false,
            bonuslist: 0,
            btnstatus: false,
            $drawtime: null,
            temptime: 0,
            breakflag: false,
            topluckywinner2: 0,
            topluckywinner: [],
            bonusinit: false,
            showtime: 10,
            countdowntimer: null,
            luckyinitindex: false,
            luckyusername: null,
            luckyuserid: [],
            startbtn: false,
            prizeshowtime: 1000,
            prizestatus: true,
            luckywinner2: 0,
            luckywinner: [],
            bonustoplist: 0,
            meslen: 0,
            rainstatus: false,
            bonusDefaultSettings: function (options) {
                var $this = this;
                var defaults={
                    countdown: 0,   //倒计时次数
                    testdata:1
                };

                var opts = $.extend(true, {}, $this.$el,
                    defaults, options || {});

                $this.$el = opts;
            },
            bonusStartEvent: function () {
                var $this = this;
                
                var bonusCountDown = temp.render("countdown-bonus-tpl", {
                    bonusImg: '../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-5.png'
                });

                if (bigScreenBonus.bonusGaming.$flag) {
                    
                    if (!bigScreenBonus.bonusGaming.breakflag) {
                        $('#bonusCountDown').html(bonusCountDown);
                        // console.log(99);
                        //临时去掉倒计时
                        bigScreenBonus.bonusGaming.bonusDefaultSettings({
                            countdown: 5
                        });

                        bigScreenBonus.bonusGaming.bonusReady(bigScreenBonus.bonusGaming.$el.countdown);
                    } else {
                        $this.luckywinner2 = 0;
                        $this.topluckywinner2 = 0;
                        $this.bonusStartGame();
                    }
                    
                }
                
            },
            bonusReady: function (num) {
                var _this = this;
                var bonusCalc = setInterval(function () {
                    --num;
                    _m.play();
                    if (num <= 0) {
                        clearInterval(bonusCalc);
                        $(".cutdown-start").show();
                        window.setTimeout(test,1000);
                        _this.luckywinner2 = 0;
                        _this.topluckywinner2 = 0;
                        bigScreenBonus.bonusGaming.bonusGameStatus(1);
                        _this.bonusStartGame();
                        return;
                    }
                    $('#bonusDetail').find('img')
                        .attr('src', '../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-'
                        + num + '.png');
                }, 1000);

            },
            bonusStartGame: function () {
                var _this = this;
                $('#bonusDetail').html('').css({
                    "background": "url(../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-bg2.png) no-repeat center",
                    "backgroundSize": "100%"
                });
                
                _this.bonusInitListData();

                _this.bonusStartInit(_this);  //开始抢红包
                
                bigScreenBonus.bonusRainLogic.$el.redenvelop = ".bonus-rain-box ul";
                _this.rainstatus = true;

                bigScreenBonus.bonusRainLogic.bonusRainHtml();
                bigScreenBonus.bonusRainLogic.bonusRainStart();
                
                _this.bonusShowTime();   //倒计时 主要部分

                _this.bonusGameRealRank();

                _this.bonusGameRealTopRank();
                
            },
            bonusInitListData: function () {
                var $this = this;
                $this.$el.bonusList = '../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-icon-list.png';
                $this.$el.bonusTopTitle = 'Top5';
                $this.$el.bonusTopList = '../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-icon-top.png';
                $this.$el.bonusListTitle = '实时榜单';
                $this.$el.bonusHead = '../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-head.png';
                $this.$el.bonusMoneypack = '../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-moneyPackage.png';

            },
            bonusGameStatus: function (status) {

                utils.ajax({
                    url: Set_RedPackstart,
                    data: {
                        isqhbshow: status
                    },
                    success: function (data) {
                        if (data.successful) {
                            if (data.code == 200) {
                                bigScreenBonus.bonusGaming.$flag = false;
                                bigScreenBonus.bonusGaming.$el.shakestatus = 2;
                            }
                        } else {
                            $.dialog.tips({
                                status: "warning",
                                content: Lang.screen.NetworkException || '有其他红包雨正在进行中...'
                            });
                        }

                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    }
                });
            },
            bonusStartInit: function (opts) {
                var bonusMainProcess = temp.render("game-bonus-tpl", {
                    bonusList: opts.$el.bonusList,
                    bonusListTitle: opts.$el.bonusListTitle,
                    bonusTopList: opts.$el.bonusTopList,
                    bonusTopTitle: opts.$el.bonusTopTitle,
                    bonusMoneypack: opts.$el.bonusMoneypack
                });
                $('#bonusDetail').html(bonusMainProcess);

            },
            bonusShowTime: function () {
                var $this = this;
                $('#second_show').html(bigScreenBonus.bonusGaming.$el.drawtime+':');
                //$('#microsecond_show').html('00');
                
                if(!bigScreenBonus.bonusGaming.breakflag)
                    $this.bonusCountTimer(bigScreenBonus.bonusGaming.$el.drawtime,$this.bonusTimerFunc);
                else $this.bonusCountTimer(bigScreenBonus.bonusGaming.temptime, $this.bonusTimerFunc);
            },
            bonusGetPrize: function () {
                var $this = this;
                var bonusPrize = temp.render("prize-bonus-tpl", {
                    bonusClose: '../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-close.png',
                    bonusListTitle: $this.$el.bonusListTitle,
                    bonusList: $this.$el.bonusList,
                    bonusHead: $this.$el.bonusHead,
                    bonusTopList: $this.$el.bonusTopList,
                    bonusTopTitle: $this.$el.bonusTopTitle
                });

                $('#bonus-prise-layout').show();
                $('#bonusDetail').append(bonusPrize);
                $("#bonus-prizeList").niceScroll({
                    cursorcolor: "#fff"
                });
                $("#bonusDetail").addClass('prizeshow');

                this.bonusGetTopPrizeData();
                this.bonusGetPrizeData();

            },
            bonusTimerFunc:function(){
                bigScreenBonus.bonusGaming.rainstatus = false;
                bigScreenBonus.bonusGaming.breakflag = false;
                bigScreenBonus.bonusGaming.bonusEndStatus();
            },
            bonusCountTimer: function (timer,callback) {
                var that = this;
                var thiTime = new Date();
                var toTime = thiTime.getTime() + timer * 1000;

                var maxSec = parseInt(timer);
                countdown.setLabels(
                    'millisecond|s|分:|小时|天| week| month| year| decade| century| millennium',
                    'milliseconds|s|分:|小时|天| weeks| months| years| decades| centuries| millennia',
                    '',
                    '',
                    '',
                    function (n) { return n.toString(); });

                that.countdowntimer = countdown(function (rs) {
                    if (!rs.toString().length > 0) {
                        $("#second_show").html('0s');
                        window.clearInterval(that.countdowntimer);
                        bigScreenBonus.bonusGaming.rainstatus = false;
                        bigScreenBonus.bonusGaming.breakflag = false;
                        bigScreenBonus.bonusGaming.bonusEndStatus();

                    } else {
                        var thisSec = parseInt((rs.toString().replace('s', '')));
                        
                        if (thisSec <= 0 || thisSec > maxSec) {
                            thisSec = 0;
                            $("#second_show").html(thisSec.toString() + 's');
                            window.clearInterval(that.countdowntimer);
                            bigScreenBonus.bonusGaming.rainstatus = false;
                            bigScreenBonus.bonusGaming.breakflag = false;
                            bigScreenBonus.bonusGaming.bonusEndStatus();
                            
                        } else {
                            $("#second_show").html(thisSec.toString() + 's');
                            maxSec = thisSec;
                        }

                        
                    }
                }, new Date(toTime), countdown.SECONDS);

            },
            bonusGetTopPrizeData: function () {
                var that = this;

                utils.ajax({
                    url: Get_RedPackLuckyTopWinners,
                    data: {
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid,
                        topn: 5
                    },
                    success: function (data) {
                        if (data.successful) {
                            var dataStr = [],
                                json = data.data,
                                len = json.length;

                            that.bonusRealRace(len, json, dataStr, '#bonus-prizeTopList');
                        }

                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    } 
                });
            },
            bonusEndStatus: function () {
                var that = this;
                // console.log(234);
                utils.ajax({
                    url: Set_RedPackend,
                    data: {
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid
                    },
                    success: function (data) {
                        // console.log(33);
                        if (data.successful) {
                            that.luckywinner2 = 1;
                            that.topluckywinner2 = 1;
                            bigScreenBonus.bonusGaming.$flag = false;
                            bigScreenBonus.bonusGaming.bonusGetPrize();
                        }

                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    }
                });
            },
            bonusGameSearchTopNotify: function (elem) {
                var that = this;
                var isOK = false;
                var count = 0;

                if (that.topluckywinner) return;
                utils.ajax({
                    url: Get_RedPackLuckyWinners,
                    data: {
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid,
                        topn: 5
                    },
                    timeout: 3000,
                    async: false,
                    success: function (data) {
                        var json = data.data;
                        var len = json.length;
                        
                        var isLen = len;

                        if (bigScreenBonus.bonusGaming.prizestatus) {

                            for (var j = (len - 1) ; j > -1; j--) {
                                var userid = json[j].id;
                                var userJson = json[j];

                                for (var k = 0; k < 9999; k++) {
                                    if (bigScreenBonus.bonusGaming.luckyuserid[k] === undefined) {
                                        bigScreenBonus.bonusGaming.luckyuserid[k] = userid;

                                        var stuats = 'real';
                                        that.bonusPrizeModal('.bonus-package p', '.bonus-package',
                                            userJson, stuats);

                                        isOK = true;
                                        break;
                                    }
                                    if (bigScreenBonus.bonusGaming.luckyuserid[k] == userid) {
                                        ++count;
                                        break;
                                    }
                                }

                                if (isOK || count == isLen) break;

                            }

                        }

                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    }
                });
            },
            bonusGameRealRank: function () {
                var that = this;
                var isOK = false;
                var count = 0;
                // console.log(bigScreenBonus.bonusGaming.meslen);
                if (that.luckywinner2) return;
                utils.ajax({
                    url: Get_RedPackLuckyWinners,
                    data: {
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid,
                        topn: 5,
                        len:bigScreenBonus.bonusGaming.meslen
                    },
                    timeout: 3000,
                    success: function (data) {

                        if (data.successful) {
                            var dataStr = [],
                                json = data.data,
                                len = json.length;

                            if (that.luckywinner[0] === undefined) {
                                that.bonusRealRace(len, json, dataStr, '.bonus-list');

                                for (var i = 0; i < len; i++) {
                                    var user = json[i];
                                    that.luckywinner[i] = user.id;
                                    bigScreenBonus.bonusGaming.meslen++
                                }
                            } else {
                                    // for (var i = 0; i < len; i++) {
                                // console.log(765);
                                        var user = json[i];
                                        bigScreenBonus.bonusGaming.meslen++
                                    // if (that.luckywinner[i] != user.id) {
                                        that.bonusRealRace(len, json, dataStr, '.bonus-list');
                                    // }
                                    
                                // }

                            }

                        }

                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    },
                    complete: function () {
                        setTimeout(function () {
                            that.bonusGameRealRank();
                        }, 1000);
                    }
                });
            },
            bonusGetPrizeData:function(){
                var that = this;
                utils.ajax({
                    url: Get_RedPackLuckyWinners,
                    data: {
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid,
                        topn:200
                    },
                    success: function (data) {
                        
                        if (data.successful) {
                            var dataStr = [],
                                json = data.data,
                                len = json.length;

                            that.bonusRealRace(len, json, dataStr, '#bonus-prizeList');
                        }

                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    }
                });
            },
            bonusPrizeModal: function (stage, anistage, elem, esnum) {
                var delay = 1000;
                bigScreenBonus.bonusGaming.prizestatus = false;

                $(stage).eq(0).find('img').attr('src', elem.avatar);
                $(stage).eq(1).html(elem.nickname);

                if (elem.prizetype == 0)
                    $(stage).eq(2).html('￥' + elem.credit/100);
                else $(stage).eq(2).html(elem.awardname);

                $(anistage).fadeIn(bigScreenBonus.bonusGaming.prizeshowtime);

                if (esnum == 'top') {
                    $(anistage).css({
                        "background":"url(../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-package2.png) no-repeat",
                        "background-size":"100%"
                    });
                    $(anistage).find('p:first').addClass('checked');
                } else {
                    $(anistage).css({
                        "background":"url(../addons/haoman_dpm/newhb_img/swallv5/img/bonus/bonus-package.png) no-repeat",
                        "background-size":"100%"
                    });
                    $(anistage).find('p:first').removeClass('checked');
                }
                
                setTimeout(function () {
                    $(anistage).fadeOut(bigScreenBonus.bonusGaming.prizeshowtime);
                    setTimeout(function () {
                        bigScreenBonus.bonusGaming.prizestatus = true;
                    }, (delay+100));
                }, delay);

            },
            bonusGameRealTopRank: function () {
                var that = this;
                var isOK = false;
                var count = 0;

                if (that.topluckywinner2) return;
                utils.ajax({
                    url: Get_RedPackLuckyTopWinners,
                    data: {
                        luckyId: bigScreenBonus.bonusGaming.$el.luckyid,
                        topn: 5
                    },
                    timeout: 3000,
                    success: function (data) {

                        if (data.successful) {
                            var dataStr = [],
                                json = data.data,
                                len = json.length;
                            
                            var isLen = len;

                            if (bigScreenBonus.bonusGaming.prizestatus) {

                                for (var j = (len - 1) ; j > -1; j--) {
                                    var userid = json[j].id;
                                    var userJson = json[j];

                                    for (var k = 0; k < 9999; k++) {
                                        if (bigScreenBonus.bonusGaming.luckyuserid[k] === undefined) {
                                            bigScreenBonus.bonusGaming.luckyuserid[k] = userid;

                                            var stuats = 'top';
                                            that.bonusPrizeModal('.bonus-package p', '.bonus-package',
                                                userJson, stuats);

                                            isOK = true;
                                            break;
                                        }
                                        if (bigScreenBonus.bonusGaming.luckyuserid[k] == userid) {
                                            ++count;
                                            break;
                                        }
                                    }

                                    if (isOK) break;

                                    if (count == isLen) {
                                        that.bonusGameSearchTopNotify(userid);
                                        break;
                                    }

                                }

                            }



                            if (that.topluckywinner[0] === undefined) {
                                that.bonusRealRace(len, json, dataStr, '.bonus-topList');

                                for (var i = 0; i < len; i++) {
                                    var user = json[i];
                                    that.topluckywinner[i] = user.id;
                                }

                            } else {

                                for (var i = 0; i < len; i++) {
                                    var user = json[i];
                                    if (that.topluckywinner[i] == user.id) {
                                        ++count;
                                        
                                    }

                                }
                                (count >=5) && that.bonusRealRace(len, json, dataStr, '.bonus-topList');

                            }

                        }

                    },
                    error: function () {
                        $.dialog.tips({
                            status: "warning",
                            content: '网络异常，获取信息失败,请重试！'
                        });
                    },
                    complete: function () {
                        setTimeout(function () {
                            that.bonusGameRealTopRank();
                        }, 1000);
                    }
                });
            },
            bonusRealRace: function (l, u, s, o) {
                for (var i = 0; i < l; i++) {
                    var user = u[i];
                    var name = user.prizepyte === 0 ? ('￥' + user.prize) : user.awardname;
                    s.push('<li><div class="bonus-head"><img src="' + user.avatar + '" /></div>' +
                        '<div class="bonus-info"><p>' + user.nickname + '</p><p>' + name + '</p></div></li>');
                }

                $(o).find('ul').html(s.join(''));
            }
        },
        bonusRainLogic: {
            $el: {},
            bonusRainHtml: function () {
                var _this=this;
                for(var i=1; i<=7; i++){
                    if(i==1){
                        for(var j=1; j<=1; j++){
                            $(_this.$el.redenvelop).append('<li class="redenvelop' + i + '" id="redenvelop' + i + '_' + j + '"></li>');
                        }
                    }

                    if(i==2){
                        for(var j=1; j<=1; j++){
                            $(_this.$el.redenvelop).append('<li class="redenvelop' + i + '" id="redenvelop' + i + '_' + j + '"></li>');
                        }
                    }

                    if(i==3){
                        for(var j=1; j<=2; j++){
                            $(_this.$el.redenvelop).append('<li class="redenvelop' + i + '" id="redenvelop' + i + '_' + j + '"></li>');
                        }
                    }

                    if(i==4){
                        for(var j=1; j<=2; j++){
                            $(_this.$el.redenvelop).append('<li class="redenvelop' + i + '" id="redenvelop' + i + '_' + j + '"></li>');
                        }
                    }

                    if(i==5){
                        for(var j=1; j<=3; j++){
                            $(_this.$el.redenvelop).append('<li class="redenvelop' + i + '" id="redenvelop' + i + '_' + j + '"></li>');
                        }
                    }

                    if(i==6){
                        for(var j=1; j<=3; j++){
                            $(_this.$el.redenvelop).append('<li class="redenvelop' + i + '" id="redenvelop' + i + '_' + j + '"></li>');
                        }
                    }

                    if(i==7){
                        for(var j=1; j<=4; j++){
                            $(_this.$el.redenvelop).append('<li class="redenvelop' + i + '" id="redenvelop' + i + '_' + j + '"></li>');
                        }
                    }
                }
            },
            bonusRainStart: function () {
                var _this = this;
                for (var i = 1; i <= 7; i++) {
                    if (i == 1) {
                        for (var j = 1; j <= 1; j++) {
                            _this.bonusRain_ani_redenvelop('#redenvelop' + i + '_' + j + '', 1500);
                        }
                    }

                    if (i == 2) {
                        for (var j = 1; j <= 1; j++) {
                            _this.bonusRain_ani_redenvelop('#redenvelop' + i + '_' + j + '', 1200);
                        }
                    }

                    if (i == 3) {
                        for (var j = 1; j <= 2; j++) {
                            _this.bonusRain_ani_redenvelop('#redenvelop' + i + '_' + j + '', 900 + (j - 1) * 400);
                        }
                    }

                    if (i == 4) {
                        for (var j = 1; j <= 2; j++) {
                            _this.bonusRain_ani_redenvelop('#redenvelop' + i + '_' + j + '', 1050 + (j - 1) * 300);
                        }
                    }

                    if (i == 5) {
                        for (var j = 1; j <= 3; j++) {
                            _this.bonusRain_ani_redenvelop('#redenvelop' + i + '_' + j + '', 1000 + (j - 1) * 200);
                        }
                    }

                    if (i == 6) {
                        for (var j = 1; j <= 3; j++) {
                            _this.bonusRain_ani_redenvelop('#redenvelop' + i + '_' + j + '', 800 + (j - 1) * 200);
                        }
                    }

                    if (i == 7) {
                        for (var j = 1; j <= 4; j++) {
                            _this.bonusRain_ani_redenvelop('#redenvelop' + i + '_' + j + '', 600 + (j - 1) * 200);
                        }
                    }

                }
            },
            bonusRain_ani_redenvelop: function (elem, time) {
                $(elem).css({
                    'top': '-160px',
                    'transition': 'top 0ms linear',
                    '-webkit-transition': 'top 0ms linear'
                });

                var mywidth = parseInt($(elem).width());

                var myleft = Math.random() * (window.innerWidth * 0.22 - mywidth);

                var myfall = window.innerHeight * 0.78 - window.innerWidth
                    * (0.11 + 0.0836) + window.innerHeight * 0.1;

                var mytime = time * 0.7 + time * 0.3 * Math.random();

                $(elem).css({
                    'left': myleft + 'px',
                    'top': myfall + 'px',
                    'transition': 'top ' + mytime + 'ms linear',
                    '-webkit-transition': 'top ' + mytime + 'ms linear'
                });

                if (bigScreenBonus.bonusGaming.rainstatus) {
                    setTimeout(function () {
                        bigScreenBonus.bonusRainLogic
                            .bonusRain_ani_redenvelop(elem, time);
                    }, mytime);
                }
                
            }

        }
        
    };
    return bigScreenBonus;
});